package com.wyg.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.wyg.dao.QuestionDao;
import com.wyg.pojo.Question;

public class QuestionDaoImpl implements QuestionDao {

	private SessionFactory sf;

	public SessionFactory getSf() {
		return sf;
	}

	public void setSf(SessionFactory sf) {
		this.sf = sf;
	}

	@Override
	public List showChoiceQuestion() {
		Session session = sf.getCurrentSession();
		Transaction trans=session.beginTransaction();
		
		String hql = "from Question q where q.type ='选择'";
		Query query = session.createQuery(hql);
		
		List list = query.list();
		trans.commit();
		return list;
	}
	
	//精选试题
	@Override
		public List showPartQuestion(){
			Session session = sf.getCurrentSession();
			Transaction trans = session.beginTransaction();
			
			String hql = "from Question q where q.id between 1 and 5";
			Query query = session.createQuery(hql);
			
			List list = query.list();
			
			trans.commit();
			return list;
		}
		
		//全部判断
		@Override
		public List showPanduanQuestion(){
			Session session = sf.getCurrentSession();
			Transaction trans = session.beginTransaction();
			
			String hql = "from Question q where q.type='判断'";
			Query query = session.createQuery(hql);
			
			List list = query.list();
			trans.commit();
			return list;
		}
		
		//显示一道题目信息
		@Override
		public Question showOneQuestion(int id){
			Session session = sf.getCurrentSession();
			Transaction t = session.beginTransaction();
			
			Question q = (Question) session.get(Question.class, id);
			t.commit();
			return q;
		}
		
		//修改选择
		public void changeChiceQuestion(int id,String title,String optionA,String optionB,String optionC,String optionD,String answer){
			Session session = sf.getCurrentSession();
			Transaction t = session.beginTransaction();
			
			Question q = (Question) session.get(Question.class, id);
			q.setTitle(title);
			q.setOptionA(optionA);
			q.setOptionB(optionB);
			q.setOptionC(optionC);
			q.setOptionD(optionD);
			q.setAnswer(answer);
			
			t.commit();
		}

}
